package everyday.m20230309;

public class Q20230318 {

    public boolean checkPalindromeFormation(String a, String b) {
        return checkPalindrome(a, b) || checkPalindrome(b, a);
    }

    private boolean checkPalindrome(String a, String b) {
        // 从前往后找相同的，直到第一个不同为止
        int left = 0;
        int right = a.length() - 1;
        while (left < right && a.charAt(left) == b.charAt(right)) {
            left ++;
            right --;
        }
        // 查看剩余的是否为回文字符串
        return checkIsPalindrome(a, left, right) || checkIsPalindrome(b, left, right);
    }

    private boolean checkIsPalindrome(String str, int left, int right) {
        while (left < right && str.charAt(left) == str.charAt(right)) {
            left ++;
            right --;
        }
        return left >= right;
    }
}
